//
//  SunView.m
//  旋转动画
//
//  Created by rimi on 16/3/15.
//  Copyright © 2016年 com.ruifeng. All rights reserved.
//

#import "SunView.h"





@implementation SunView


//设置旋转速速
- (NSInteger)speed{
    if (!_speed) {
        _speed = 12;
    }
    return _speed;
}
//蒙板
- (CAShapeLayer *)maskLayer{
    if (!_maskLayer) {
        _maskLayer = [CAShapeLayer layer];
        _maskLayer.bounds = CGRectMake(0, 0, self.frame.size.width*0.9, self.frame.size.width*0.9);
        _maskLayer.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
        _maskLayer.lineWidth = self.frame.size.width*0.35;
        //        _maskLayer.path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(20, 20) radius:20 startAngle:-M_PI/2 endAngle:M_PI*3/2 clockwise:YES].CGPath;
        UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, self.frame.size.width*0.9, self.frame.size.width*0.9)];
        _maskLayer.path = circlePath.CGPath;
    
        _maskLayer.strokeColor = [UIColor whiteColor].CGColor;
        _maskLayer.fillColor = [UIColor clearColor].CGColor;
        
    }
    return _maskLayer;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self start];
    }
    return self;
}

- (void)start{
    
    _sun = [[UIImageView alloc]init];
    _sun.bounds = CGRectMake(0, 0, self.frame.size.width*0.1, self.frame.size.width*0.1);
    _sun.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
    NSLog(@"%f",self.center.x);
    _sun.alpha = 1;
    _sun.image = [UIImage imageNamed:@"shape2"];
    [self addSubview:_sun];


//    太阳图标完整的
    self.sunAll = [[UIImageView alloc]init];
    self.sunAll.bounds = CGRectMake(0, 0, self.frame.size.width*0.9, self.frame.size.width*0.9);
    self.sunAll.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
    if (_speed == 12) {
        self.sunAll.image = [UIImage imageNamed:@"多云太阳"];
    }else{
        self.sunAll.image = [UIImage imageNamed:@"shape"];
    
    }

    self.sunAll.alpha = 0;
    [self addSubview:self.sunAll];
    [UIView animateWithDuration:1 animations:^{
        self.sun.bounds = CGRectMake(0, 0, self.frame.size.width*0.9, self.frame.size.width*0.9);
        self.sun.alpha = 1;
    } completion:^(BOOL finished){
        [UIView animateWithDuration:0.2 animations:^{
            CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeStart"];
            animation.duration = 2;
            animation.fromValue = @0;
            animation.toValue = @1;
            animation.removedOnCompletion = NO;
            animation.fillMode = kCAFillModeForwards;
            [self.layer addSublayer:self.maskLayer];
            [self.maskLayer addAnimation:animation forKey:@"strokeStart"];
            //蒙板放上去后再将太阳对透明度改成1
            self.sunAll.alpha = 1;
        } completion:^(BOOL finished){
            CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
            rotationAnimation.duration = self.speed;
            rotationAnimation.toValue = @(2*M_PI);
            rotationAnimation.repeatCount = HUGE_VAL;
            [_sunAll.layer addAnimation:rotationAnimation forKey:@"transform.rotation"];
        }];
    }];
    
    
 
}

//控件消失响应动画
- (void)disMiss{
    [UIView animateWithDuration:0.5 animations:^{
        self.transform = CGAffineTransformMakeScale(0.1, 0.1);
        self.alpha = 0;
    } completion:^(BOOL finished){
        [self removeFromSuperview];
    }];
}




@end

